{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `BasisMatrices.jl`: Examples\n",
    "\n",
    "This is an overview of how to use the main features of the package. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting up a Basis Structure\n",
    "\n",
    "Let's suppose that we want to set up a two-dimensional basis over grids for two variables, $y$ and $a$. Create the grids as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ygrid0 = linspace(-4, 4, 10)\n",
    "agrid0 = linspace(0.0.^0.4, 100.0.^0.4, 25).^(1/0.4);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "We first need to create a `Basis` object. This will contain the information about the polynomial family, the bounds, the interpolation nodes, and anything else specific to the family of polynomials chosen. \n",
    "\n",
    "The package lets us use different types of polynomials for different dimensions. Let's use a Chebyshev polynomial in the $y$ direction, and a spline in the $a$ dimension. We can do this in two ways."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2 dimensional Basis on the hypercube formed by (0.0,-4.0) × (100.00000000000003,4.0).\n",
       "Basis families are Spline × Cheb\n"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using BasisMatrices\n",
    "\n",
    "# 1st method -- combining Basis objects\n",
    "y_basis = Basis(ChebParams(length(ygrid0), minimum(ygrid0), maximum(ygrid0)))\n",
    "a_basis = Basis(SplineParams(agrid0, 0, 3))\n",
    "basis = Basis(a_basis, y_basis)\n",
    "\n",
    "# 2nd method -- combining Params\n",
    "basis = Basis(SplineParams(agrid0, 0, 3),\n",
    "              ChebParams(length(ygrid0), minimum(ygrid0), maximum(ygrid0)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "In the first method, we construct the basis in each dimension separately, then put them together using the `Basis` constructor. The second method puts together the basis in one step from each of the `BasisParams` objects: here, these are `ChebParams(...)` and `SplineParams(...)`. \n",
    "\n",
    "The fields for the `BasisParams` objects will depend on the type of polynomial. For a Chebyshev polynomial, it needs to know the number of points, and the lower and upper endpoints. For a spline, it needs to know the breakpoints, an indicator that is non-zero if the breakpoints are evenly spaced, and the degree.\n",
    "\n",
    "After constructing a `Basis` type, we may want to be able to extract the interpolation nodes that were computed. To do this, simply use the `nodes` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "S, (agrid, ygrid) = nodes(basis);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`S` gives you the tensor product of the nodes in each dimension, and `agrid` and `ygrid` give you the vector of nodes in each dimension on their own."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using the `BasisMatrix`\n",
    "\n",
    "Let's say we want to approximate the function $f(a, y) = \\sqrt(a) \\exp(y)$ using our `Basis` as defined above. Creating a `BasisMatrix` object allows us to evaluate the basis functions at the desired points. To create it, use the following constructor. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BasisMatrix{BasisMatrices.Expanded} of order [0 0]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Φ = BasisMatrix(basis, Expanded(), S, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The representation type above is `Expanded()`, which means the output will be expanded along both the rows and columns (this is easiest, conceptually, for finding the coefficients). Here, `S` is the desired set of evaluation points, and `0` is the order of derivative (we could have also done `1` to get the first derivative, for example). The field `vals` gives us the actual basis matrix. To extract the coefficients, $c$, we just need to solve the system $f(a, y) = \\Phi c$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Actual function at interpolation nodes\n",
    "f(a::Vector{Float64}, y::Vector{Float64}) = sqrt.(a) .* exp.(y)\n",
    "y = f(S[:,1], S[:, 2])\n",
    "\n",
    "# Get coefficients\n",
    "c = Φ.vals[1] \\ y;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have the coefficients, we can evaluate the interpolant at any points using `funeval`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "using QuantEcon\n",
    "\n",
    "ygridf = linspace(-4, 4, 100)\n",
    "agridf = linspace(0.0, 100.0, 250)\n",
    "Sf = gridmake(agridf, ygridf)\n",
    "yf = f(Sf[:, 1], Sf[:, 2])\n",
    "interp = funeval(c, basis, Sf);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, lets plot both the interpolant and the original function, holding $y$ fixed, then holding $a$ fixed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using Plots\n",
    "plotlyjs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"05058f3a-40ca-4f0c-a34a-0914cfac423b\" class=\"plotly-graph-div\"></div>\n",
       "\n",
       "<script>\n",
       "    window.PLOTLYENV=window.PLOTLYENV || {};\n",
       "    window.PLOTLYENV.BASE_URL=\"https://plot.ly\";\n",
       "    require(['plotly'], function(Plotly) {\n",
       "        Plotly.newPlot('05058f3a-40ca-4f0c-a34a-0914cfac423b', [{\"yaxis\":\"y\",\"y\":[0.0,0.011278565634279562,0.015912907542164633,0.019486258342359845,0.022506524971144425,0.025157192060279727,0.027559651217494496,0.029767700084201106,0.03182128471247162,0.033752858400781835,0.03557900980548032,0.03731430721473148,0.03897306721316666,0.04056517670682719,0.04209673786914475,0.04357373116117673,0.04500213703386182,0.046387355540019805,0.04773264333836675,0.04904076241754523,0.05031447476620797,0.05155654237299982,0.05276968745497865,0.05395592379802559,0.055116655357821155,0.05625326617760642,0.05736714030055629,0.058459661769886755,0.0595322145351996,0.06058604247520934,0.06162196798268985,0.06264073449385235,0.06364308544495548,0.0646297642722444,0.06560151441190526,0.06655907930020945,0.06750319149939864,0.06843441273232319,0.06935316892232056,0.07025988224249756,0.07115497486600479,0.07203886896598288,0.07291198671558313,0.0737747502879443,0.07462758185621202,0.07547087656879911,0.07630491568243605,0.07712995045604162,0.07794623214835694,0.07875401201811177,0.07955354132406173,0.08034507132497205,0.0811288532795621,0.08190513844657671,0.08267417788747032,0.08343619368587718,0.08419134866719136,0.08493979841342843,0.085681698506535,0.08641720452854058,0.0871464720614491,0.08786965668721419,0.08858691398786674,0.08929839954536863,0.09000426894172778,0.09070467753858484,0.09139976171471138,0.09208962452314559,0.09277436564005517,0.09345408474154143,0.09412888150376018,0.09479885560287137,0.0954641067149856,0.09612473451622841,0.0967808386827661,0.09743251889072745,0.09807987481627402,0.09872300612900455,0.09936200490557393,0.09999694097345839,0.1006278801062532,0.10125488807746953,0.10187803066062474,0.10249737362930766,0.10311298275701558,0.10372492381730673,0.1043332625837365,0.10493806482979497,0.10553939632906403,0.10613732285508136,0.10673191018136406,0.10732322314921575,0.10791131468891334,0.10849622950397468,0.10907801213421355,0.10965670711938942,0.11023235899924087,0.11080501231365643,0.11137471160237247,0.1119415014051999,0.1125054262619245,0.11306653071231501,0.11362485929617963,0.11418045655330342,0.11473336702350816,0.11528363524654756,0.11583130434297587,0.11637640858308321,0.11691897883216845,0.11745904594829941,0.11799664078952465,0.11853179421394644,0.11906453707964974,0.11959490024464275,0.12012291456714098,0.12064861090509878,0.12117202011669186,0.12169317305993947,0.12221210059294654,0.12272883357376639,0.12324340286050245,0.12375583931126088,0.12426617326209384,0.12477442970411984,0.12528063049467042,0.12578479745133567,0.12628695239187596,0.12678711713382776,0.1272853134948536,0.12778156329258966,0.12827588834466824,0.1287683104687381,0.12925885148244126,0.12974753320337862,0.13023437744920432,0.1307194060375516,0.1312026407860599,0.13168410351237114,0.1321638160340915,0.13264180016567587,0.1331180761983086,0.13359266047391508,0.13406556870251507,0.1345368165942887,0.1350064198591974,0.13547439420741697,0.13594075534901615,0.136405518994116,0.13686870085271693,0.1373303166350003,0.13779038205102923,0.138248912810849,0.1387059246245796,0.1391614332023172,0.13961545425414507,0.14006800349014703,0.14051909662045353,0.14096874935507062,0.14141697740175793,0.14186379541106878,0.14230921531076518,0.14275324859641747,0.1431959067635471,0.14363720130772856,0.1440771437244263,0.14451574550917878,0.14495301815762207,0.14538897316521668,0.14582362202751678,0.14625697624004363,0.14668904729834392,0.14711984669794198,0.14754938593441488,0.147977676503272,0.1484047299000656,0.14883055762029992,0.1492551711595348,0.14967858201331988,0.15010080167713608,0.15052184149754752,0.1509417111212065,0.15136041911384093,0.15177797402378795,0.15219438439952304,0.15260965878934654,0.15302380574166372,0.15343683380490852,0.15384875152742142,0.15425956745756247,0.15466929014376024,0.15507792813438753,0.15548548997781933,0.15589198422244657,0.1562974194166381,0.15670180410879558,0.15710514684730387,0.15750745618052897,0.15790874065686913,0.15830900882468263,0.1587082692323556,0.15910653042834172,0.1595038008496259,0.15990008769213368,0.16029539737574156,0.1606897363084796,0.16108311089823257,0.16147552755295805,0.16186699268071114,0.16225751268943217,0.16264709398705457,0.16303574298158538,0.163423466080979,0.16381026969324197,0.1641961602262816,0.16458114408813762,0.16496522768674396,0.16534841743007417,0.16573071972604578,0.16611214098277347,0.16649268760807812,0.16687236601002692,0.1672511825965277,0.16762914377557248,0.16800625595514096,0.16838252554266558,0.16875795860110837,0.16913256026282247,0.16950633550433922,0.16987928930224888,0.17025142663315745,0.1706227524736048,0.1709932718002754,0.1713629895896761,0.17173191081840244,0.17210004046300778,0.17246738350019386,0.17283394490641493,0.17319972965829875,0.1735647427325085,0.173928989105528,0.1742924737540207,0.17465520165453388,0.1750171777836541,0.1753784071179576,0.17573889463404543,0.17609864530851604,0.17645766411793085,0.17681595603888667,0.1771735260479674,0.17753037912176647],\"showlegend\":true,\"name\":\"Interpolant\",\"type\":\"scatter\",\"xaxis\":\"x\",\"line\":{\"width\":2,\"dash\":\"solid\",\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\"},\"x\":[0.0,0.40160642570281124,0.8032128514056225,1.2048192771084338,1.606425702811245,2.0080321285140563,2.4096385542168677,2.8112449799196786,3.21285140562249,3.6144578313253013,4.016064257028113,4.417670682730924,4.819277108433735,5.220883534136546,5.622489959839357,6.024096385542169,6.42570281124498,6.827309236947791,7.228915662650603,7.6305220883534135,8.032128514056225,8.433734939759036,8.835341365461847,9.236947791164658,9.63855421686747,10.040160642570282,10.441767068273093,10.843373493975903,11.244979919678714,11.646586345381525,12.048192771084338,12.449799196787149,12.85140562248996,13.25301204819277,13.654618473895582,14.056224899598394,14.457831325301205,14.859437751004016,15.261044176706827,15.662650602409638,16.06425702811245,16.46586345381526,16.867469879518072,17.269076305220885,17.670682730923694,18.072289156626507,18.473895582329316,18.87550200803213,19.27710843373494,19.67871485943775,20.080321285140563,20.481927710843372,20.883534136546185,21.285140562248998,21.686746987951807,22.08835341365462,22.48995983935743,22.89156626506024,23.29317269076305,23.694779116465863,24.096385542168676,24.497991967871485,24.899598393574298,25.301204819277107,25.70281124497992,26.104417670682732,26.50602409638554,26.907630522088354,27.309236947791163,27.710843373493976,28.11244979919679,28.514056224899598,28.91566265060241,29.31726907630522,29.718875502008032,30.120481927710845,30.522088353413654,30.923694779116467,31.325301204819276,31.72690763052209,32.1285140562249,32.53012048192771,32.93172690763052,33.333333333333336,33.734939759036145,34.136546184738954,34.53815261044177,34.93975903614458,35.34136546184739,35.7429718875502,36.144578313253014,36.54618473895582,36.94779116465863,37.34939759036145,37.75100401606426,38.15261044176707,38.55421686746988,38.95582329317269,39.3574297188755,39.75903614457831,40.16064257028113,40.562248995983936,40.963855421686745,41.36546184738956,41.76706827309237,42.16867469879518,42.570281124497996,42.971887550200805,43.373493975903614,43.77510040160642,44.17670682730924,44.57831325301205,44.97991967871486,45.381526104417674,45.78313253012048,46.18473895582329,46.5863453815261,46.98795180722892,47.389558232931726,47.791164658634536,48.19277108433735,48.59437751004016,48.99598393574297,49.397590361445786,49.799196787148595,50.200803212851405,50.602409638554214,51.00401606425703,51.40562248995984,51.80722891566265,52.208835341365464,52.610441767068274,53.01204819277108,53.4136546184739,53.81526104417671,54.21686746987952,54.618473895582326,55.02008032128514,55.42168674698795,55.82329317269076,56.22489959839358,56.626506024096386,57.028112449799195,57.429718875502004,57.83132530120482,58.23293172690763,58.63453815261044,59.036144578313255,59.437751004016064,59.83935742971887,60.24096385542169,60.6425702811245,61.04417670682731,61.44578313253012,61.84738955823293,62.24899598393574,62.65060240963855,63.05220883534137,63.45381526104418,63.855421686746986,64.2570281124498,64.65863453815261,65.06024096385542,65.46184738955823,65.86345381526104,66.26506024096386,66.66666666666667,67.06827309236948,67.46987951807229,67.8714859437751,68.27309236947791,68.67469879518072,69.07630522088354,69.47791164658635,69.87951807228916,70.28112449799197,70.68273092369478,71.08433734939759,71.4859437751004,71.88755020080322,72.28915662650603,72.69076305220884,73.09236947791165,73.49397590361446,73.89558232931726,74.29718875502007,74.6987951807229,75.1004016064257,75.50200803212851,75.90361445783132,76.30522088353413,76.70682730923694,77.10843373493977,77.51004016064257,77.91164658634538,78.3132530120482,78.714859437751,79.11646586345381,79.51807228915662,79.91967871485944,80.32128514056225,80.72289156626506,81.12449799196787,81.52610441767068,81.92771084337349,82.3293172690763,82.73092369477912,83.13253012048193,83.53413654618474,83.93574297188755,84.33734939759036,84.73895582329317,85.14056224899599,85.5421686746988,85.94377510040161,86.34538152610442,86.74698795180723,87.14859437751004,87.55020080321285,87.95180722891567,88.35341365461848,88.75502008032129,89.1566265060241,89.5582329317269,89.95983935742971,90.36144578313252,90.76305220883535,91.16465863453816,91.56626506024097,91.96787148594377,92.36947791164658,92.7710843373494,93.1726907630522,93.57429718875503,93.97590361445783,94.37751004016064,94.77911646586345,95.18072289156626,95.58232931726907,95.9839357429719,96.3855421686747,96.78714859437751,97.18875502008032,97.59036144578313,97.99196787148594,98.39357429718875,98.79518072289157,99.19678714859438,99.59839357429719,100.0],\"mode\":\"lines\"},{\"yaxis\":\"y\",\"y\":[0.0,0.011607064527640048,0.01641486807432822,0.020104025488603017,0.023214129055280096,0.025954185303029634,0.028431385504276777,0.03070940619161496,0.03282973614865644,0.03482119358292015,0.036704760855888974,0.038496277955625775,0.040208050977206033,0.041849866312025405,0.04342965872860617,0.04495396761390477,0.04642825811056019,0.047857153050819876,0.04924460422298466,0.05059402130713864,0.05190837060605927,0.053190251794147374,0.05444195838573038,0.055665525954781005,0.05686277100855355,0.05803532263820024,0.05918464852196723,0.06031207646580904,0.06141881238322992,0.06250595540838647,0.063574510682065,0.06462540023406275,0.06565947229731288,0.0666775093214376,0.06768023490103442,0.06866831979310399,0.0696423871658403,0.0706030171954825,0.0715507511075488,0.07248609474239316,0.07340952171177795,0.07432147620237906,0.07522237547332307,0.07611261208760607,0.07699255591125155,0.07786255590908892,0.07872294176188283,0.07957402532606728,0.08041610195441207,0.08124945169348034,0.0820743403716411,0.08289102058961992,0.08369973262405081,0.08450070525318944,0.08529415651283033,0.08608029438950587,0.08685931745721234,0.08763141546318645,0.08839676986762804,0.08915555434171892,0.08990793522780954,0.09065407196522832,0.09139411748480093,0.09212821857484488,0.09285651622112039,0.09357914592296723,0.09429623798763552,0.09500791780462087,0.09571430610163975,0.09641551918372473,0.0971116691567805,0.09780286413681756,0.09848920844596933,0.09917080279629883,0.09984774446231219,0.10052012744301508,0.10118804261427727,0.1018515778722038,0.10251081826815349,0.10316584613599208,0.10381674121211854,0.10446358074876044,0.10510643962099368,0.10574539042790657,0.10638050358829475,0.10701184743124469,0.10763948828193486,0.10826349054296001,0.10888391677146075,0.1095008277523197,0.11011428256766691,0.11072433866291904,0.11133105190956201,0.11193447666487029,0.1125346658287445,0.11313167089783475,0.1137255420171071,0.11431632802899852,0.11490407652029752,0.11548883386687732,0.11607064527640049,0.11664955482910679,0.11722560551678797,0.1177988392800472,0.11836929704393447,0.11893701875204368,0.11950204339915192,0.12006440906247609,0.12062415293161809,0.12118131133726515,0.12173591977870783,0.12228801295023478,0.12283762476645985,0.12338478838663375,0.12392953623798965,0.12447190003816921,0.12501191081677293,0.12554959893607623,0.12608499411095037,0.12661812542802547,0.12714902136413,0.12767770980404053,0.12820421805757246,0.12872857287604175,0.1292508004681255,0.12977092651514818,0.13028897618581853,0.1308049741504412,0.13131894459462576,0.13183091123251475,0.13234089731955118,0.13284892566480463,0.1333550186428752,0.13385919820539188,0.13436148589212307,0.13486190284171432,0.13536046980206884,0.1358572071403854,0.13635213485286685,0.13684527257411264,0.13733663958620798,0.13782625482752112,0.13831413690122057,0.13880030408352276,0.1392847743316806,0.13976756529172277,0.1402486943059535,0.14072817842022112,0.141206034390965,0.1416822786920481,0.14215692752138387,0.14262999680736443,0.1431015022150976,0.14357145915245964,0.14403988277597002,0.14450678799649513,0.14497218948478632,0.14543610167685872,0.14589853877921596,0.14635951477392642,0.1468190434235559,0.14727713827596187,0.14773381266895397,0.1481890797348252,0.1486429524047581,0.14909544341311035,0.14954656530158353,0.1499963304232793,0.15044475094664614,0.15089183885932064,0.15133760597186668,0.1517820639214159,0.15222522417521214,0.15266709803406361,0.15310769663570536,0.15354703095807481,0.1539851118225031,0.15442194989682517,0.15485755569841073,0.15529193959711876,0.15572511181817783,0.15615708244499443,0.15658786142189177,0.15701745855678081,0.15744588352376565,0.1578731458656854,0.1582992549965943,0.15872422020418175,0.15914805065213455,0.15957075538244211,0.15999234331764747,0.1604128232630446,0.16083220390882413,0.16125049383216905,0.1616677014993013,0.1620838352674812,0.16249890338696069,0.16291291400289168,0.16332587515719113,0.1637377947903635,0.1641486807432822,0.1645585407589311,0.16496738248410678,0.16537521347108333,0.16578204117923984,0.16618787297665247,0.16659271614165125,0.16699657786434302,0.16739946524810162,0.16780138531102531,0.1682023449873633,0.16860235112891156,0.16900141050637887,0.16939952981072404,0.16979671565446483,0.17019297457295932,0.17058831302566066,0.1709827373973454,0.1713762539993169,0.17176886907058328,0.17216058877901175,0.17255141922245895,0.17294136642987873,0.1733304363624069,0.17371863491442469,0.17410596791460073,0.17449244112691178,0.17487806025164387,0.1752628309263729,0.17564675872692662,0.1760298491683271,0.17641210770571497,0.17679353973525608,0.17717415059503003,0.1775539455659017,0.17793292987237627,0.17831110868343783,0.17868848711337162,0.17906507022257126,0.1794408630183303,0.1798158704556191,0.18019009743784695,0.18056354881761041,0.18093622939742715,0.18130814393045663,0.18167929712120745,0.18204969362623158,0.18241933805480598,0.18278823496960186,0.1831563888873418],\"showlegend\":true,\"name\":\"Original\",\"type\":\"scatter\",\"xaxis\":\"x\",\"line\":{\"width\":2,\"dash\":\"solid\",\"color\":\"rgba(227, 111, 71, 1.000)\",\"shape\":\"linear\"},\"x\":[0.0,0.40160642570281124,0.8032128514056225,1.2048192771084338,1.606425702811245,2.0080321285140563,2.4096385542168677,2.8112449799196786,3.21285140562249,3.6144578313253013,4.016064257028113,4.417670682730924,4.819277108433735,5.220883534136546,5.622489959839357,6.024096385542169,6.42570281124498,6.827309236947791,7.228915662650603,7.6305220883534135,8.032128514056225,8.433734939759036,8.835341365461847,9.236947791164658,9.63855421686747,10.040160642570282,10.441767068273093,10.843373493975903,11.244979919678714,11.646586345381525,12.048192771084338,12.449799196787149,12.85140562248996,13.25301204819277,13.654618473895582,14.056224899598394,14.457831325301205,14.859437751004016,15.261044176706827,15.662650602409638,16.06425702811245,16.46586345381526,16.867469879518072,17.269076305220885,17.670682730923694,18.072289156626507,18.473895582329316,18.87550200803213,19.27710843373494,19.67871485943775,20.080321285140563,20.481927710843372,20.883534136546185,21.285140562248998,21.686746987951807,22.08835341365462,22.48995983935743,22.89156626506024,23.29317269076305,23.694779116465863,24.096385542168676,24.497991967871485,24.899598393574298,25.301204819277107,25.70281124497992,26.104417670682732,26.50602409638554,26.907630522088354,27.309236947791163,27.710843373493976,28.11244979919679,28.514056224899598,28.91566265060241,29.31726907630522,29.718875502008032,30.120481927710845,30.522088353413654,30.923694779116467,31.325301204819276,31.72690763052209,32.1285140562249,32.53012048192771,32.93172690763052,33.333333333333336,33.734939759036145,34.136546184738954,34.53815261044177,34.93975903614458,35.34136546184739,35.7429718875502,36.144578313253014,36.54618473895582,36.94779116465863,37.34939759036145,37.75100401606426,38.15261044176707,38.55421686746988,38.95582329317269,39.3574297188755,39.75903614457831,40.16064257028113,40.562248995983936,40.963855421686745,41.36546184738956,41.76706827309237,42.16867469879518,42.570281124497996,42.971887550200805,43.373493975903614,43.77510040160642,44.17670682730924,44.57831325301205,44.97991967871486,45.381526104417674,45.78313253012048,46.18473895582329,46.5863453815261,46.98795180722892,47.389558232931726,47.791164658634536,48.19277108433735,48.59437751004016,48.99598393574297,49.397590361445786,49.799196787148595,50.200803212851405,50.602409638554214,51.00401606425703,51.40562248995984,51.80722891566265,52.208835341365464,52.610441767068274,53.01204819277108,53.4136546184739,53.81526104417671,54.21686746987952,54.618473895582326,55.02008032128514,55.42168674698795,55.82329317269076,56.22489959839358,56.626506024096386,57.028112449799195,57.429718875502004,57.83132530120482,58.23293172690763,58.63453815261044,59.036144578313255,59.437751004016064,59.83935742971887,60.24096385542169,60.6425702811245,61.04417670682731,61.44578313253012,61.84738955823293,62.24899598393574,62.65060240963855,63.05220883534137,63.45381526104418,63.855421686746986,64.2570281124498,64.65863453815261,65.06024096385542,65.46184738955823,65.86345381526104,66.26506024096386,66.66666666666667,67.06827309236948,67.46987951807229,67.8714859437751,68.27309236947791,68.67469879518072,69.07630522088354,69.47791164658635,69.87951807228916,70.28112449799197,70.68273092369478,71.08433734939759,71.4859437751004,71.88755020080322,72.28915662650603,72.69076305220884,73.09236947791165,73.49397590361446,73.89558232931726,74.29718875502007,74.6987951807229,75.1004016064257,75.50200803212851,75.90361445783132,76.30522088353413,76.70682730923694,77.10843373493977,77.51004016064257,77.91164658634538,78.3132530120482,78.714859437751,79.11646586345381,79.51807228915662,79.91967871485944,80.32128514056225,80.72289156626506,81.12449799196787,81.52610441767068,81.92771084337349,82.3293172690763,82.73092369477912,83.13253012048193,83.53413654618474,83.93574297188755,84.33734939759036,84.73895582329317,85.14056224899599,85.5421686746988,85.94377510040161,86.34538152610442,86.74698795180723,87.14859437751004,87.55020080321285,87.95180722891567,88.35341365461848,88.75502008032129,89.1566265060241,89.5582329317269,89.95983935742971,90.36144578313252,90.76305220883535,91.16465863453816,91.56626506024097,91.96787148594377,92.36947791164658,92.7710843373494,93.1726907630522,93.57429718875503,93.97590361445783,94.37751004016064,94.77911646586345,95.18072289156626,95.58232931726907,95.9839357429719,96.3855421686747,96.78714859437751,97.18875502008032,97.59036144578313,97.99196787148594,98.39357429718875,98.79518072289157,99.19678714859438,99.59839357429719,100.0],\"mode\":\"lines\"},{\"yaxis\":\"y2\",\"y\":[0.022506524971144425,0.025481181618229237,0.027983754170836494,0.030268764951833314,0.03252968520807347,0.03491055504917741,0.037516073245288176,0.04042030132943691,0.04367411788035405,0.047311550558100844,0.05135510542797429,0.05582020533794559,0.06071884161085449,0.06606253607550798,0.07186470348996596,0.07814249770609275,0.08491821848673749,0.09222034971582686,0.10008429383691572,0.1085528617176981,0.11767657176640788,0.12751380702095838,0.13813087409320368,0.14960200327869524,0.1620093248368575,0.17544285240757113,0.19000050075783725,0.2057881615461856,0.22291985755333382,0.24151799285465794,0.2617137137038393,0.2836473924573214,0.3074692446959296,0.3333400877934275,0.3614322475415039,0.39193061806720514,0.4250338791716429,0.4609558743782619,0.4999271524049726,0.542196674466853,0.5880336897753249,0.6377297818250651,0.6916010885521199,0.749990700205395,0.8132712397986133,0.8818476323017181,0.9561600702888631,1.0366871855847881,1.1239494385427833,1.2185127389451331,1.320992315141315,1.4320568509301241,1.5524329128492138,1.682909693959513,1.824344103902445,1.9776662389647977,2.14388527010972,2.324095791422152,2.5194846761739234,2.7313384927364397,2.9610515378588027,3.2101345503853898,3.4802241743092734,3.7730932461476305,4.090661987980443,4.435010194116628,4.808390506240142,5.213242879044234,5.652210345783567,6.128156200862868,6.644182724535066,7.2036515830038,7.810206045713086,8.467795170361253,9.180700115197572,9.953562747447535,10.791416726266647,11.699721248443526,12.684397655160186,13.75186910847096,14.909103556781986,16.163660219499626,17.523739832170005,18.998238904851004,20.596808258145078,22.329916113273633,24.208916024793375,26.24611995704116,28.45487681814574,30.849656778464418,33.44614171358832,36.26132212561097,39.31360091017469,42.62290435089404,46.21080073710669,50.10062701552109,54.31762390121464,58.88907988858694,63.844484618291716,69.21569207185328],\"showlegend\":false,\"name\":\"\",\"type\":\"scatter\",\"xaxis\":\"x2\",\"line\":{\"width\":2,\"dash\":\"solid\",\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\"},\"x\":[-4.0,-3.919191919191919,-3.8383838383838382,-3.757575757575758,-3.676767676767677,-3.595959595959596,-3.515151515151515,-3.4343434343434343,-3.3535353535353534,-3.272727272727273,-3.191919191919192,-3.111111111111111,-3.0303030303030303,-2.9494949494949494,-2.8686868686868685,-2.787878787878788,-2.707070707070707,-2.6262626262626263,-2.5454545454545454,-2.4646464646464645,-2.3838383838383836,-2.303030303030303,-2.2222222222222223,-2.1414141414141414,-2.0606060606060606,-1.97979797979798,-1.898989898989899,-1.8181818181818181,-1.7373737373737375,-1.6565656565656566,-1.5757575757575757,-1.494949494949495,-1.4141414141414141,-1.3333333333333333,-1.2525252525252526,-1.1717171717171717,-1.0909090909090908,-1.0101010101010102,-0.9292929292929293,-0.8484848484848485,-0.7676767676767676,-0.6868686868686869,-0.6060606060606061,-0.5252525252525253,-0.4444444444444444,-0.36363636363636365,-0.2828282828282828,-0.20202020202020202,-0.12121212121212122,-0.04040404040404041,0.04040404040404041,0.12121212121212122,0.20202020202020202,0.2828282828282828,0.36363636363636365,0.4444444444444444,0.5252525252525253,0.6060606060606061,0.6868686868686869,0.7676767676767676,0.8484848484848485,0.9292929292929293,1.0101010101010102,1.0909090909090908,1.1717171717171717,1.2525252525252526,1.3333333333333333,1.4141414141414141,1.494949494949495,1.5757575757575757,1.6565656565656566,1.7373737373737375,1.8181818181818181,1.898989898989899,1.97979797979798,2.0606060606060606,2.1414141414141414,2.2222222222222223,2.303030303030303,2.3838383838383836,2.4646464646464645,2.5454545454545454,2.6262626262626263,2.707070707070707,2.787878787878788,2.8686868686868685,2.9494949494949494,3.0303030303030303,3.111111111111111,3.191919191919192,3.272727272727273,3.3535353535353534,3.4343434343434343,3.515151515151515,3.595959595959596,3.676767676767677,3.757575757575758,3.8383838383838382,3.919191919191919,4.0],\"mode\":\"lines\"},{\"yaxis\":\"y2\",\"y\":[0.023214129055280096,0.025167895271033944,0.02728609593172135,0.029582570301458246,0.03207232239564702,0.03477161900971181,0.037698095998207184,0.040870873498674715,0.04431068085306251,0.048039992042881736,0.052083172522966015,0.05646663841317156,0.06121902908809761,0.06637139429244164,0.07195739700450787,0.07801353337327749,0.08457937116599995,0.09169780828420188,0.09941535303712884,0.10778242800378605,0.1168536994688605,0.12668843458489462,0.13735088859423036,0.14891072464063762,0.16144346891346775,0.17503100409801498,0.1897621043560448,0.20573301533178384,0.223048082972841,0.2418204352744613,0.26217272140128756,0.2842379130156838,0.30816017304809756,0.33409579758557245,0.36221423703224026,0.39269920321354446,0.4257498696574603,0.4615821728947488,0.5004302232802914,0.5425478345531037,0.5882101821284119,0.6377156009562405,0.6913875346928225,0.7495766489197433,0.8126631222175332,0.8810591300624415,0.9552115377749305,1.0356048201142674,1.122764226594386,1.2172592132016116,1.3197071629353745,1.4307774194800633,1.551195660362023,1.6817486381637017,1.8232893207716898,1.9767424642424438,2.1431106546960264,2.323480858712568,2.5190315250284687,2.731040283931275,2.96089229465722,3.2100892953291598,3.4802594145626724,3.773167808844439,4.090728195182236,4.435015354375093,4.8082786865937415,5.2129569078367375,5.651693983281551,6.127356401631172,6.643051903318197,7.202149784927082,7.808302912493534,8.465471587504942,9.177949421530306,9.950391388531639,10.787844238136577,11.695779468577282,12.680129074724418,13.747324304775901,14.904337678817333,16.158728543782267,17.518692462445774,18.99311475913422,20.59162857199216,22.32467779109086,24.20358529358459,26.240626921729,28.449111687097098,30.843468725006975,33.43934156727811,36.2536903492472,39.30490261881325,42.61291347148254,46.19933579631613,50.08760148374149,54.30311451780975,58.873416953126686,63.8283688608678,69.20034341955466],\"showlegend\":false,\"name\":\"\",\"type\":\"scatter\",\"xaxis\":\"x2\",\"line\":{\"width\":2,\"dash\":\"solid\",\"color\":\"rgba(227, 111, 71, 1.000)\",\"shape\":\"linear\"},\"x\":[-4.0,-3.919191919191919,-3.8383838383838382,-3.757575757575758,-3.676767676767677,-3.595959595959596,-3.515151515151515,-3.4343434343434343,-3.3535353535353534,-3.272727272727273,-3.191919191919192,-3.111111111111111,-3.0303030303030303,-2.9494949494949494,-2.8686868686868685,-2.787878787878788,-2.707070707070707,-2.6262626262626263,-2.5454545454545454,-2.4646464646464645,-2.3838383838383836,-2.303030303030303,-2.2222222222222223,-2.1414141414141414,-2.0606060606060606,-1.97979797979798,-1.898989898989899,-1.8181818181818181,-1.7373737373737375,-1.6565656565656566,-1.5757575757575757,-1.494949494949495,-1.4141414141414141,-1.3333333333333333,-1.2525252525252526,-1.1717171717171717,-1.0909090909090908,-1.0101010101010102,-0.9292929292929293,-0.8484848484848485,-0.7676767676767676,-0.6868686868686869,-0.6060606060606061,-0.5252525252525253,-0.4444444444444444,-0.36363636363636365,-0.2828282828282828,-0.20202020202020202,-0.12121212121212122,-0.04040404040404041,0.04040404040404041,0.12121212121212122,0.20202020202020202,0.2828282828282828,0.36363636363636365,0.4444444444444444,0.5252525252525253,0.6060606060606061,0.6868686868686869,0.7676767676767676,0.8484848484848485,0.9292929292929293,1.0101010101010102,1.0909090909090908,1.1717171717171717,1.2525252525252526,1.3333333333333333,1.4141414141414141,1.494949494949495,1.5757575757575757,1.6565656565656566,1.7373737373737375,1.8181818181818181,1.898989898989899,1.97979797979798,2.0606060606060606,2.1414141414141414,2.2222222222222223,2.303030303030303,2.3838383838383836,2.4646464646464645,2.5454545454545454,2.6262626262626263,2.707070707070707,2.787878787878788,2.8686868686868685,2.9494949494949494,3.0303030303030303,3.111111111111111,3.191919191919192,3.272727272727273,3.3535353535353534,3.4343434343434343,3.515151515151515,3.595959595959596,3.676767676767677,3.757575757575758,3.8383838383838382,3.919191919191919,4.0],\"mode\":\"lines\"}],\n",
       "               {\"yaxis\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[0.0,0.05,0.1,0.15000000000000002],\"domain\":[0.08664541932258472,0.9887514060742407],\"ticktext\":[\"0.00\",\"0.05\",\"0.10\",\"0.15\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x\"},\"annotations\":[],\"width\":800,\"showlegend\":true,\"legend\":{\"bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"y\":1.0,\"font\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"bordercolor\":\"rgba(0, 0, 0, 1.000)\",\"x\":1.0},\"xaxis\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"a\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[0.0,20.0,40.0,60.0,80.0,100.0],\"domain\":[0.04936570428696413,0.5061898512685914],\"ticktext\":[\"0\",\"20\",\"40\",\"60\",\"80\",\"100\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y\"},\"yaxis2\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[20.0,40.0,60.0],\"domain\":[0.08664541932258472,0.9887514060742407],\"ticktext\":[\"20\",\"40\",\"60\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x2\"},\"xaxis2\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"y\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[-4.0,-2.0,0.0,2.0,4.0],\"domain\":[0.538254593175853,0.9950787401574803],\"ticktext\":[\"-4\",\"-2\",\"0\",\"2\",\"4\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y2\"},\"margin\":{\"r\":0,\"l\":0,\"b\":0,\"t\":20},\"height\":350,\"plot_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"paper_bgcolor\":\"rgba(255, 255, 255, 1.000)\"}, {showLink: false});\n",
       "\n",
       "    });\n",
       " </script>\n"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plt_a = plot(agridf, [interp[1:length(agridf)] yf[1:length(agridf)]], \n",
    "             label=[\"Interpolant\" \"Original\"],\n",
    "             lw=2,\n",
    "             xlabel=\"a\")\n",
    "plt_y = plot(ygridf, [interp[5:length(agridf):end] yf[5:length(agridf):end]], \n",
    "             label=\"\",\n",
    "             lw=2,\n",
    "             xlabel=\"y\")\n",
    "plot(plt_a, plt_y, layout=(1, 2), size=(800, 350))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Now, let's suppose we want to change the interpolation range of `y`, but keep the values of `a` the same. We can do this with minimal calculations by taking advantage of the `Direct` representation of the `BasisMatrix`. We just need to store the `a` part of the interpolation and then compute the new `y` part separately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Φ_direct = BasisMatrix(basis, Direct(), S, [0 0])\n",
    "Φ_a = Φ_direct.vals[1]\n",
    "ygrid_new = linspace(-8, 8, 10)\n",
    "S_new = gridmake(agrid, ygrid_new)\n",
    "Φ_direct_new = BasisMatrix(basis[2], Direct(), S_new[:,2], 0)\n",
    "Φ_y = Φ_direct_new.vals[1]\n",
    "Φ_new = row_kron(Φ_y, Φ_a);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Above, we stored only the `a` part of the interpolation, from the first element of `vals` in the `Direct` representation. Then, we defined a new set of `y`'s as well as the complete corresponding set of evaluation points, `S_new`. We used these points to get a new `Direct` representation, then extracted the `y` part of this. The final basis matrix for the original set of `a` and the new set of `y` is just the tensor product of the two. Again, we can compute and plot the original function and its interpolant using this new basis matrix:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"5ea5719b-bf08-4f7a-b5a0-6093113f0195\" class=\"plotly-graph-div\"></div>\n",
       "\n",
       "<script>\n",
       "    window.PLOTLYENV=window.PLOTLYENV || {};\n",
       "    window.PLOTLYENV.BASE_URL=\"https://plot.ly\";\n",
       "    require(['plotly'], function(Plotly) {\n",
       "        Plotly.newPlot('5ea5719b-bf08-4f7a-b5a0-6093113f0195', [{\"yaxis\":\"y\",\"y\":[0.0,0.00021312055361377702,0.0003006913976371095,0.00036821368045836245,0.0004252848467842796,0.0004753720348066384,0.0005207690689967848,0.0005624925123157709,0.0006012971891365265,0.000637796338387453,0.0006723034213678147,0.0007050937204269303,0.000736437763593667,0.000766522374371803,0.0007954628596920088,0.0008233722260087006,0.0008503634822979186,0.0008765386667477504,0.0009019593180283891,0.0009266776264169607,0.0009507457828448196,0.0009742159782319959,0.0009971396515138786,0.0010195548549649658,0.0010414881186662228,0.0010629655944853766,0.001084013436397413,0.001104657797696973,0.0011249248288040853,0.0011448380340954856,0.001164412953257532,0.0011836636359916497,0.0012026041284087796,0.0012212484797657908,0.0012396107379686327,0.0012577049494666426,0.0012755449595961699,0.001293141379686169,0.001310502260308155,0.001327635577666797,0.001344549309344245,0.0013612514334084835,0.001377749926348315,0.0013940527663862667,0.0014101679306577353,0.0014261028852616264,0.001441862946301331,0.0014574528602722125,0.0014728773744003831,0.0014881412383900283,0.001503249199188872,0.0015182060049596657,0.0015330164026750026,0.0015476851410198833,0.0015622169630810099,0.0015766160658916561,0.00159088552568587,0.0016050282846560293,0.0016190472802861677,0.0016329454516750275,0.0016467257416223902,0.0016603910889614326,0.0016739444313031981,0.0016873887120709696,0.0017007268693873812,0.0017139618381538674,0.0017270961971093124,0.0017401318921685949,0.001753070807907875,0.0017659148275654957,0.0017786658324894233,0.0017913257102009084,0.0018038963410589304,0.001816379608897023,0.0018287773989129619,0.0018410915936897254,0.0018533240753413338,0.0018654767289210405,0.0018775512933874583,0.001889549089326703,0.0019014713586988918,0.0019133193441405183,0.0019250942889854628,0.0019367974357749063,0.001948430028534176,0.0019599933092990796,0.001971488520013054,0.0019829169040406214,0.0019942797065937157,0.002005578167143085,0.002016813529621686,0.002027987020154963,0.0020390996391396188,0.002050152229491786,0.0020611456346539825,0.002072080694880185,0.0020829582525294654,0.0020937791488648827,0.0021045442269524983,0.0021152543259006507,0.0021259102893509407,0.0021365129570654062,0.002147063173083552,0.0021575617770395183,0.0021680096119212067,0.0021784075184996254,0.002188756310992357,0.002199056637877339,0.0022093090819015337,0.002219514228785968,0.0022296726569983605,0.0022397849518895896,0.0022498516990552275,0.002259873477981955,0.0022698508725937927,0.0022797844677313606,0.0022896748428422597,0.0022995225854742785,0.0023093282745207944,0.002319092497089059,0.002328815833493536,0.0023384988688803787,0.002348142173811657,0.0023577462221194834,0.0023673114279669782,0.0023768381999227373,0.002386326955990725,0.0023957781031356262,0.0024051920557681683,0.0024145692268504604,0.0024239100271188363,0.0024332148692991495,0.00244248416564119,0.002451718327591834,0.0024609177716001795,0.002470082904778792,0.0024792141421698943,0.002488311895149309,0.002497376576229726,0.0025064085971478792,0.0025154083430520835,0.0025243761195061776,0.002533312225668549,0.0025422169584210508,0.002551090614144158,0.0025599334905521687,0.0025687458827121645,0.0025775280885724783,0.002586280405864727,0.002595003127169093,0.0026036965541038626,0.002612360985210671,0.002620996711243606,0.0026296040333022574,0.002638183246688186,0.0026467346481808818,0.0026552585344461477,0.0026637552040256196,0.00267222495144421,0.002680668055325991,0.0026890847397063045,0.002697475224791912,0.0027058397225792064,0.0027141784545072767,0.0027224916370087726,0.0027307794868889346,0.0027390422203694698,0.0027472800564254385,0.0027554932119784326,0.0027636819034171367,0.002771846348082363,0.002779986762078579,0.0027881033650487552,0.0027961963732536788,0.0028042660045457524,0.0028123124731678217,0.002820336001150281,0.0028283368008032994,0.0028363150915993174,0.002844271090430575,0.0028522049763515245,0.002860116909451982,0.0028680070564401916,0.00287587557720137,0.002883722630709684,0.0028915483807203657,0.0028993529868088785,0.0029071366127411125,0.0029148994200340894,0.0029226415674514783,0.0029303632194057627,0.0029380645358401125,0.0029457456789430125,0.002953406810888737,0.0029610480909809667,0.00296866968511722,0.002976271749901116,0.0029838544487574836,0.00299141794357638,0.0029989623959636447,0.0030064879667861533,0.0030139948150700105,0.0030214830791296388,0.0030289528767979412,0.0030364043333763746,0.003043837566659491,0.0030512526979400434,0.0030586498517592986,0.00306602914362486,0.0030733906988160697,0.0030807346369527977,0.0030880610783867724,0.0030953701462692607,0.003102661959786701,0.003109936641067179,0.0031171943114145506,0.003124435090910538,0.0031316591018821782,0.003138866463928025,0.0031460572982950907,0.003153231726969352,0.003160389871482039,0.0031675318505222094,0.003174657789941193,0.0031817678066756758,0.0031888620150980485,0.003195940512900286,0.0032030033959244975,0.003210050756603522,0.00321708268835863,0.0032240992872942797,0.003231100646843288,0.003238086860818612,0.003245058021946079,0.003252014222823618,0.0032589555644335633,0.0032658821335189714,0.0032727940284758006,0.0032796913408219552,0.00328657416457645,0.0032934425962594105,0.003300296727957175,0.0033071366533476976,0.0033139624670184276,0.003320774266285298,0.003327572137095558,0.0033343561817673617,0.00334112648943119,0.00334788315831247,0.003354626279360673],\"showlegend\":true,\"name\":\"Interpolant\",\"type\":\"scatter\",\"xaxis\":\"x\",\"line\":{\"width\":2,\"dash\":\"solid\",\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\"},\"x\":[0.0,0.40160642570281124,0.8032128514056225,1.2048192771084338,1.606425702811245,2.0080321285140563,2.4096385542168677,2.8112449799196786,3.21285140562249,3.6144578313253013,4.016064257028113,4.417670682730924,4.819277108433735,5.220883534136546,5.622489959839357,6.024096385542169,6.42570281124498,6.827309236947791,7.228915662650603,7.6305220883534135,8.032128514056225,8.433734939759036,8.835341365461847,9.236947791164658,9.63855421686747,10.040160642570282,10.441767068273093,10.843373493975903,11.244979919678714,11.646586345381525,12.048192771084338,12.449799196787149,12.85140562248996,13.25301204819277,13.654618473895582,14.056224899598394,14.457831325301205,14.859437751004016,15.261044176706827,15.662650602409638,16.06425702811245,16.46586345381526,16.867469879518072,17.269076305220885,17.670682730923694,18.072289156626507,18.473895582329316,18.87550200803213,19.27710843373494,19.67871485943775,20.080321285140563,20.481927710843372,20.883534136546185,21.285140562248998,21.686746987951807,22.08835341365462,22.48995983935743,22.89156626506024,23.29317269076305,23.694779116465863,24.096385542168676,24.497991967871485,24.899598393574298,25.301204819277107,25.70281124497992,26.104417670682732,26.50602409638554,26.907630522088354,27.309236947791163,27.710843373493976,28.11244979919679,28.514056224899598,28.91566265060241,29.31726907630522,29.718875502008032,30.120481927710845,30.522088353413654,30.923694779116467,31.325301204819276,31.72690763052209,32.1285140562249,32.53012048192771,32.93172690763052,33.333333333333336,33.734939759036145,34.136546184738954,34.53815261044177,34.93975903614458,35.34136546184739,35.7429718875502,36.144578313253014,36.54618473895582,36.94779116465863,37.34939759036145,37.75100401606426,38.15261044176707,38.55421686746988,38.95582329317269,39.3574297188755,39.75903614457831,40.16064257028113,40.562248995983936,40.963855421686745,41.36546184738956,41.76706827309237,42.16867469879518,42.570281124497996,42.971887550200805,43.373493975903614,43.77510040160642,44.17670682730924,44.57831325301205,44.97991967871486,45.381526104417674,45.78313253012048,46.18473895582329,46.5863453815261,46.98795180722892,47.389558232931726,47.791164658634536,48.19277108433735,48.59437751004016,48.99598393574297,49.397590361445786,49.799196787148595,50.200803212851405,50.602409638554214,51.00401606425703,51.40562248995984,51.80722891566265,52.208835341365464,52.610441767068274,53.01204819277108,53.4136546184739,53.81526104417671,54.21686746987952,54.618473895582326,55.02008032128514,55.42168674698795,55.82329317269076,56.22489959839358,56.626506024096386,57.028112449799195,57.429718875502004,57.83132530120482,58.23293172690763,58.63453815261044,59.036144578313255,59.437751004016064,59.83935742971887,60.24096385542169,60.6425702811245,61.04417670682731,61.44578313253012,61.84738955823293,62.24899598393574,62.65060240963855,63.05220883534137,63.45381526104418,63.855421686746986,64.2570281124498,64.65863453815261,65.06024096385542,65.46184738955823,65.86345381526104,66.26506024096386,66.66666666666667,67.06827309236948,67.46987951807229,67.8714859437751,68.27309236947791,68.67469879518072,69.07630522088354,69.47791164658635,69.87951807228916,70.28112449799197,70.68273092369478,71.08433734939759,71.4859437751004,71.88755020080322,72.28915662650603,72.69076305220884,73.09236947791165,73.49397590361446,73.89558232931726,74.29718875502007,74.6987951807229,75.1004016064257,75.50200803212851,75.90361445783132,76.30522088353413,76.70682730923694,77.10843373493977,77.51004016064257,77.91164658634538,78.3132530120482,78.714859437751,79.11646586345381,79.51807228915662,79.91967871485944,80.32128514056225,80.72289156626506,81.12449799196787,81.52610441767068,81.92771084337349,82.3293172690763,82.73092369477912,83.13253012048193,83.53413654618474,83.93574297188755,84.33734939759036,84.73895582329317,85.14056224899599,85.5421686746988,85.94377510040161,86.34538152610442,86.74698795180723,87.14859437751004,87.55020080321285,87.95180722891567,88.35341365461848,88.75502008032129,89.1566265060241,89.5582329317269,89.95983935742971,90.36144578313252,90.76305220883535,91.16465863453816,91.56626506024097,91.96787148594377,92.36947791164658,92.7710843373494,93.1726907630522,93.57429718875503,93.97590361445783,94.37751004016064,94.77911646586345,95.18072289156626,95.58232931726907,95.9839357429719,96.3855421686747,96.78714859437751,97.18875502008032,97.59036144578313,97.99196787148594,98.39357429718875,98.79518072289157,99.19678714859438,99.59839357429719,100.0],\"mode\":\"lines\"},{\"yaxis\":\"y\",\"y\":[0.0,0.00021259080244649114,0.0003006487960556071,0.00036821807105916063,0.0004251816048929823,0.00047536748566158276,0.0005207389900027251,0.0005624623942930773,0.0006012975921112142,0.0006377724073394734,0.0006722711453338081,0.0007050839255955799,0.0007364361421183213,0.0007665070389128591,0.0007954419463341132,0.0008233606374321313,0.0008503632097859646,0.0008765343335217003,0.0009019463881668214,0.0009266618241904742,0.0009507349713231655,0.0009742134442624488,0.0009971392501885313,0.0010195496719392289,0.0010414779800054501,0.0010629540122324558,0.001084004650085007,0.0011046542131774818,0.0011249247885861545,0.001144836507655328,0.0011644077801806765,0.001183655493727011,0.0012025951842224284,0.0012212411827316584,0.0012396067423520504,0.0012577041484266107,0.001275544814678947,0.0012931393674075473,0.0013104977195035612,0.0013276291357562464,0.0013445422906676163,0.001361245319800426,0.0013777458655221603,0.0013940511178748972,0.0014101678511911598,0.0014261024569847483,0.0014418609735694973,0.0014574491127952365,0.0014728722842366425,0.001488135617125438,0.0015032439802780356,0.0015182020002381085,0.0015330140778257182,0.0015476844032607812,0.001562216970008175,0.0015766155874741207,0.0015908838926682264,0.00160502536093236,0.0016190433158260141,0.0016329409382478406,0.0016467212748642625,0.0016603872459084428,0.0016739416524061605,0.0016873871828792318,0.0017007264195719291,0.0017139618442412294,0.0017270958435476706,0.0017401307140799747,0.0017530686670434005,0.0017659118326389255,0.0017786622641578168,0.0017913219418138815,0.0018038927763336429,0.0018163766123228794,0.0018287752314263181,0.001841090355295803,0.0018533236483809484,0.0018654767205550738,0.0018775511295881545,0.0018895483834775436,0.001901469942646331,0.0019133172220184205,0.0019250915929786632,0.001936794385225745,0.0019484268885248976,0.0019599903543669945,0.0019714859975400534,0.001982914997618744,0.0019942785003770626,0.00200557761912897,0.0020168134360014243,0.002027987003143934,0.0020390993438784578,0.002050151453793207,0.0020611443017836584,0.0020720788310438594,0.0020829559600109003,0.0020937765832652186,0.00210454157238925,0.0021152517767867394,0.0021259080244649116,0.0021365111227815187,0.0021470618591586943,0.0021575610017653805,0.002168009300170014,0.002178407485965038,0.002188756273364707,0.002199056359777576,0.0022093084263549636,0.002219513138516618,0.002229671146454726,0.0022397830856173495,0.002249849577172309,0.0022598712284524667,0.0022698486333833155,0.0022797823728937256,0.002289673015310656,0.0022995211167385773,0.002309327221424343,0.002319091862108166,0.002328815560361353,0.0023384988269114023,0.002348142161955031,0.0023577460554596824,0.002367310987454022,0.0023768374283079136,0.0023863258390023397,0.0023957766713896904,0.002405190368444857,0.0024145673645075113,0.0024239080855159487,0.0024332129492328526,0.002442482365463317,0.0024517167362654523,0.0024609164561538786,0.0024700819122963937,0.0024792134847041007,0.0024883115464152585,0.0024973764636730955,0.0025064085960978468,0.0025154082968532215,0.0025243759128075333,0.0025333117846896994,0.0025422162472402993,0.002551089629357894,0.0025599322542407717,0.002568744439524314,0.0025775264974141243,0.0025862787348150946,0.0025950014534565505,0.0026036949500136248,0.002612359516224996,0.0026209954390071224,0.002629603000565101,0.0026381824785002696,0.0026467341459146723,0.002655258271512493,0.002663755119698572,0.0026722249506741,0.0026806680205295917,0.0026890845813352326,0.0026974748812286886,0.002705839164500464,0.002714177671676895,0.002722490639600852,0.0027307783015102305,0.0027390408871143094,0.0027472786226680366,0.0027554917310443207,0.002763680431804385,0.0027718449412662523,0.0027799854725714225,0.0027881022357497945,0.0027961954377828894,0.002804265282665441,0.0028123119714653865,0.0028203357023823196,0.0028283366708044527,0.0028363150693641315,0.002844271087991948,0.0028522049139694966,0.00286011673198081,0.00286800672416252,0.002875875070152783,0.0028837219471389945,0.0028915475299043517,0.0028993519908732716,0.0029071355001557193,0.002914898225590473,0.0029226403327873465,0.0029303619851684145,0.0029380633440082627,0.002945744568473285,0.002953405815660067,0.002961047240632872,0.0029686689964602635,0.002976271234250876,0.0029838541031883704,0.0029914177505655937,0.002998962321817959,0.0030064879605560713,0.0030139948085976275,0.0030214830059985924,0.003028952691083691,0.003036404000476217,0.003043837069127193,0.0030512520303438823,0.0030586490158176867,0.0030660281556514363,0.003073389578386084,0.0030807334110268345,0.003088059779068708,0.0030953688065215625,0.0031026606159345826,0.0031099353284202566,0.0031171930636778415,0.00312443394001635,0.003131658074377044,0.0031388655823554757,0.0031460565782230653,0.0031532311749482415,0.0031603894842171442,0.003167531616453915,0.003174657680840565,0.003181767785336453,0.0031888620366973667,0.0031959405404942257,0.003203003401131408,0.00321005072186472,0.0032170826048190074,0.003224099151005424,0.003231100460338356,0.0032380866316520283,0.0032450577627167786,0.003252013950255021,0.003258955289956907,0.003265881876495681,0.003272793803542746,0.0032796911637824433,0.003286574048926554,0.003293442549728525,0.003300296755997431,0.0033071367566116783,0.003313962639532446,0.0033207744918168857,0.0033275723996310795,0.003334356448262751,0.0033411267221337515,0.003347883304812321,0.0033546262790251184],\"showlegend\":true,\"name\":\"Original\",\"type\":\"scatter\",\"xaxis\":\"x\",\"line\":{\"width\":2,\"dash\":\"solid\",\"color\":\"rgba(227, 111, 71, 1.000)\",\"shape\":\"linear\"},\"x\":[0.0,0.40160642570281124,0.8032128514056225,1.2048192771084338,1.606425702811245,2.0080321285140563,2.4096385542168677,2.8112449799196786,3.21285140562249,3.6144578313253013,4.016064257028113,4.417670682730924,4.819277108433735,5.220883534136546,5.622489959839357,6.024096385542169,6.42570281124498,6.827309236947791,7.228915662650603,7.6305220883534135,8.032128514056225,8.433734939759036,8.835341365461847,9.236947791164658,9.63855421686747,10.040160642570282,10.441767068273093,10.843373493975903,11.244979919678714,11.646586345381525,12.048192771084338,12.449799196787149,12.85140562248996,13.25301204819277,13.654618473895582,14.056224899598394,14.457831325301205,14.859437751004016,15.261044176706827,15.662650602409638,16.06425702811245,16.46586345381526,16.867469879518072,17.269076305220885,17.670682730923694,18.072289156626507,18.473895582329316,18.87550200803213,19.27710843373494,19.67871485943775,20.080321285140563,20.481927710843372,20.883534136546185,21.285140562248998,21.686746987951807,22.08835341365462,22.48995983935743,22.89156626506024,23.29317269076305,23.694779116465863,24.096385542168676,24.497991967871485,24.899598393574298,25.301204819277107,25.70281124497992,26.104417670682732,26.50602409638554,26.907630522088354,27.309236947791163,27.710843373493976,28.11244979919679,28.514056224899598,28.91566265060241,29.31726907630522,29.718875502008032,30.120481927710845,30.522088353413654,30.923694779116467,31.325301204819276,31.72690763052209,32.1285140562249,32.53012048192771,32.93172690763052,33.333333333333336,33.734939759036145,34.136546184738954,34.53815261044177,34.93975903614458,35.34136546184739,35.7429718875502,36.144578313253014,36.54618473895582,36.94779116465863,37.34939759036145,37.75100401606426,38.15261044176707,38.55421686746988,38.95582329317269,39.3574297188755,39.75903614457831,40.16064257028113,40.562248995983936,40.963855421686745,41.36546184738956,41.76706827309237,42.16867469879518,42.570281124497996,42.971887550200805,43.373493975903614,43.77510040160642,44.17670682730924,44.57831325301205,44.97991967871486,45.381526104417674,45.78313253012048,46.18473895582329,46.5863453815261,46.98795180722892,47.389558232931726,47.791164658634536,48.19277108433735,48.59437751004016,48.99598393574297,49.397590361445786,49.799196787148595,50.200803212851405,50.602409638554214,51.00401606425703,51.40562248995984,51.80722891566265,52.208835341365464,52.610441767068274,53.01204819277108,53.4136546184739,53.81526104417671,54.21686746987952,54.618473895582326,55.02008032128514,55.42168674698795,55.82329317269076,56.22489959839358,56.626506024096386,57.028112449799195,57.429718875502004,57.83132530120482,58.23293172690763,58.63453815261044,59.036144578313255,59.437751004016064,59.83935742971887,60.24096385542169,60.6425702811245,61.04417670682731,61.44578313253012,61.84738955823293,62.24899598393574,62.65060240963855,63.05220883534137,63.45381526104418,63.855421686746986,64.2570281124498,64.65863453815261,65.06024096385542,65.46184738955823,65.86345381526104,66.26506024096386,66.66666666666667,67.06827309236948,67.46987951807229,67.8714859437751,68.27309236947791,68.67469879518072,69.07630522088354,69.47791164658635,69.87951807228916,70.28112449799197,70.68273092369478,71.08433734939759,71.4859437751004,71.88755020080322,72.28915662650603,72.69076305220884,73.09236947791165,73.49397590361446,73.89558232931726,74.29718875502007,74.6987951807229,75.1004016064257,75.50200803212851,75.90361445783132,76.30522088353413,76.70682730923694,77.10843373493977,77.51004016064257,77.91164658634538,78.3132530120482,78.714859437751,79.11646586345381,79.51807228915662,79.91967871485944,80.32128514056225,80.72289156626506,81.12449799196787,81.52610441767068,81.92771084337349,82.3293172690763,82.73092369477912,83.13253012048193,83.53413654618474,83.93574297188755,84.33734939759036,84.73895582329317,85.14056224899599,85.5421686746988,85.94377510040161,86.34538152610442,86.74698795180723,87.14859437751004,87.55020080321285,87.95180722891567,88.35341365461848,88.75502008032129,89.1566265060241,89.5582329317269,89.95983935742971,90.36144578313252,90.76305220883535,91.16465863453816,91.56626506024097,91.96787148594377,92.36947791164658,92.7710843373494,93.1726907630522,93.57429718875503,93.97590361445783,94.37751004016064,94.77911646586345,95.18072289156626,95.58232931726907,95.9839357429719,96.3855421686747,96.78714859437751,97.18875502008032,97.59036144578313,97.99196787148594,98.39357429718875,98.79518072289157,99.19678714859438,99.59839357429719,100.0],\"mode\":\"lines\"},{\"yaxis\":\"y2\",\"y\":[0.0004252848467842796,4.547836267027037,6.939473882403917,7.8041656474337024,7.637759938211076,6.824257155348227,5.654439435830813,4.34220989623499,3.038840955866631,1.8453201554017808,0.8229710564478672,0.0025162801282785363,-0.6082604816943019,-1.018106570595261,-1.2470092880228507,-1.321893666835141,-1.2731901239348566,-1.1321617403175819,-0.9288893223337272,-0.6908205026159422,-0.4417969409427032,-0.20148118429224804,0.014888058507357513,0.19647657880910072,0.3366290276522888,0.4324884458853459,0.4845064532327345,0.4958839026496376,0.4719764271585643,0.41969423004630735,0.3469206958173165,0.26196992865100877,0.17309915729539313,0.08808808034792207,0.013893663726559256,-0.04361435718022158,-0.07983497467660208,-0.0915347628749198,-0.07686679721630314,-0.035314395829196064,0.03243381569510358,0.12466957221783818,0.2389022862880322,0.3721380946805865,0.5211901804084772,0.6830096854015184,0.8550266008994793,1.0354903972959906,1.2238008326918313,1.4208203597722324,1.6291608338121182,1.853438810636328,2.100495612218432,2.379580529291977,2.7024980248717485,3.083722599940006,3.5404880827436838,4.092861506172882,4.763815443549341,5.579316681845081,6.568453422876844,7.763627817380667,9.200845554063624,10.920139445755447,12.96616947764283,15.389047608261931,18.245441742451774,21.600019726831476,25.52730095255807,30.113990187149433,35.46187559701807,41.691380565055525,48.945866853136536,57.39679490777318,67.24985565834483,78.75219701135887,92.20087740106055,107.95268821640394,126.4354966869302,148.1612708754578,173.740958793691,203.9014043278779,239.50449363552045,281.5687369508306,331.29350231616075,390.08612963900094,459.5921656593356,541.7289729001783,638.7229784649819,753.1508416393004,887.9848326506358,1046.6427286397475,1233.0425468989176,1451.662449737687,1707.6061699444588,2006.674320723049,2355.441969196811,2761.3428680893257,3232.76075600986,3779.1281528937875],\"showlegend\":false,\"name\":\"\",\"type\":\"scatter\",\"xaxis\":\"x2\",\"line\":{\"width\":2,\"dash\":\"solid\",\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\"},\"x\":[-8.0,-7.838383838383838,-7.6767676767676765,-7.515151515151516,-7.353535353535354,-7.191919191919192,-7.03030303030303,-6.8686868686868685,-6.707070707070707,-6.545454545454546,-6.383838383838384,-6.222222222222222,-6.0606060606060606,-5.898989898989899,-5.737373737373737,-5.575757575757576,-5.414141414141414,-5.252525252525253,-5.090909090909091,-4.929292929292929,-4.767676767676767,-4.606060606060606,-4.444444444444445,-4.282828282828283,-4.121212121212121,-3.95959595959596,-3.797979797979798,-3.6363636363636362,-3.474747474747475,-3.313131313131313,-3.1515151515151514,-2.98989898989899,-2.8282828282828283,-2.6666666666666665,-2.505050505050505,-2.3434343434343434,-2.1818181818181817,-2.0202020202020203,-1.8585858585858586,-1.696969696969697,-1.5353535353535352,-1.3737373737373737,-1.2121212121212122,-1.0505050505050506,-0.8888888888888888,-0.7272727272727273,-0.5656565656565656,-0.40404040404040403,-0.24242424242424243,-0.08080808080808081,0.08080808080808081,0.24242424242424243,0.40404040404040403,0.5656565656565656,0.7272727272727273,0.8888888888888888,1.0505050505050506,1.2121212121212122,1.3737373737373737,1.5353535353535352,1.696969696969697,1.8585858585858586,2.0202020202020203,2.1818181818181817,2.3434343434343434,2.505050505050505,2.6666666666666665,2.8282828282828283,2.98989898989899,3.1515151515151514,3.313131313131313,3.474747474747475,3.6363636363636362,3.797979797979798,3.95959595959596,4.121212121212121,4.282828282828283,4.444444444444445,4.606060606060606,4.767676767676767,4.929292929292929,5.090909090909091,5.252525252525253,5.414141414141414,5.575757575757576,5.737373737373737,5.898989898989899,6.0606060606060606,6.222222222222222,6.383838383838384,6.545454545454546,6.707070707070707,6.8686868686868685,7.03030303030303,7.191919191919192,7.353535353535354,7.515151515151516,7.6767676767676765,7.838383838383838,8.0],\"mode\":\"lines\"},{\"yaxis\":\"y2\",\"y\":[0.0004251816048929823,0.0004997622797687671,0.000587425075321733,0.0006904647130959974,0.0008115784294186404,0.0009539365801102879,0.0011212656298965095,0.0013179456989078487,0.0015491252197126718,0.0018208557062240741,0.002140250162284317,0.002515669276538681,0.0029569402775636573,0.003475614178151608,0.004085268142554977,0.004801860891662692,0.0056441504494387906,0.006634185165840881,0.007797880869571688,0.009165699258610436,0.01077344528654056,0.012663204417605909,0.014884444284727585,0.017495309588238098,0.020564144131491692,0.024171279835200194,0.028411139560962365,0.0333947087889397,0.0392524408500111,0.04613767176175879,0.05423063405737716,0.06374317467624732,0.07492430041492114,0.08806669610004805,0.10351438610741653,0.12167173978029439,0.14301405647908297,0.1681000073438158,0.19758625945362124,0.23224466519519146,0.27298246680097293,0.32086604494494614,0.3771488330555494,0.44330412804997266,0.5210636563661376,0.6124629048233042,0.719894402923046,0.8461703513446966,0.9945962360417818,1.1690573549125802,1.3741205220263726,1.6151536116850547,1.8984660715875754,2.231474083266301,2.622894693148313,3.0829739959496467,3.623755343487702,4.259394599729911,5.00653069441135,5.884721175087277,6.916954158930258,8.130250085473762,9.556369022195307,11.232642037855896,13.202948406195283,15.518864219938111,18.24101248202007,21.4406500149493,25.201532728332225,29.62210807108787,34.81809205948556,40.92549833906165,48.10419857122072,56.54210734364612,66.46010115160871,78.11780021280745,91.8203644644978,107.92648163959625,126.85775652312765,149.10974716864052,175.26493696617172,206.00798212751874,242.14363371746964,284.61780337038596,334.5423241228057,393.22405451862613,462.19908783591444,543.2729619195569,638.5679221799504,750.5784749464452,882.236685378275,1036.9889292159357,1218.886107479582,1432.689685636324,1683.996332990523,1979.3844249433528,2326.5862430653983,2734.6903806096543,3214.3796517706683,3778.210732365951],\"showlegend\":false,\"name\":\"\",\"type\":\"scatter\",\"xaxis\":\"x2\",\"line\":{\"width\":2,\"dash\":\"solid\",\"color\":\"rgba(227, 111, 71, 1.000)\",\"shape\":\"linear\"},\"x\":[-8.0,-7.838383838383838,-7.6767676767676765,-7.515151515151516,-7.353535353535354,-7.191919191919192,-7.03030303030303,-6.8686868686868685,-6.707070707070707,-6.545454545454546,-6.383838383838384,-6.222222222222222,-6.0606060606060606,-5.898989898989899,-5.737373737373737,-5.575757575757576,-5.414141414141414,-5.252525252525253,-5.090909090909091,-4.929292929292929,-4.767676767676767,-4.606060606060606,-4.444444444444445,-4.282828282828283,-4.121212121212121,-3.95959595959596,-3.797979797979798,-3.6363636363636362,-3.474747474747475,-3.313131313131313,-3.1515151515151514,-2.98989898989899,-2.8282828282828283,-2.6666666666666665,-2.505050505050505,-2.3434343434343434,-2.1818181818181817,-2.0202020202020203,-1.8585858585858586,-1.696969696969697,-1.5353535353535352,-1.3737373737373737,-1.2121212121212122,-1.0505050505050506,-0.8888888888888888,-0.7272727272727273,-0.5656565656565656,-0.40404040404040403,-0.24242424242424243,-0.08080808080808081,0.08080808080808081,0.24242424242424243,0.40404040404040403,0.5656565656565656,0.7272727272727273,0.8888888888888888,1.0505050505050506,1.2121212121212122,1.3737373737373737,1.5353535353535352,1.696969696969697,1.8585858585858586,2.0202020202020203,2.1818181818181817,2.3434343434343434,2.505050505050505,2.6666666666666665,2.8282828282828283,2.98989898989899,3.1515151515151514,3.313131313131313,3.474747474747475,3.6363636363636362,3.797979797979798,3.95959595959596,4.121212121212121,4.282828282828283,4.444444444444445,4.606060606060606,4.767676767676767,4.929292929292929,5.090909090909091,5.252525252525253,5.414141414141414,5.575757575757576,5.737373737373737,5.898989898989899,6.0606060606060606,6.222222222222222,6.383838383838384,6.545454545454546,6.707070707070707,6.8686868686868685,7.03030303030303,7.191919191919192,7.353535353535354,7.515151515151516,7.6767676767676765,7.838383838383838,8.0],\"mode\":\"lines\"}],\n",
       "               {\"yaxis\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[0.0,0.001,0.002,0.003],\"domain\":[0.08664541932258472,0.9887514060742407],\"ticktext\":[\"0.000\",\"0.001\",\"0.002\",\"0.003\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x\"},\"annotations\":[],\"width\":800,\"showlegend\":true,\"legend\":{\"bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"y\":1.0,\"font\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"bordercolor\":\"rgba(0, 0, 0, 1.000)\",\"x\":1.0},\"xaxis\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"a\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[0.0,20.0,40.0,60.0,80.0,100.0],\"domain\":[0.060476815398075236,0.5006342957130359],\"ticktext\":[\"0\",\"20\",\"40\",\"60\",\"80\",\"100\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y\"},\"yaxis2\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[0.0,1000.0,2000.0,3000.0],\"domain\":[0.08664541932258472,0.9887514060742407],\"ticktext\":[\"0\",\"1000\",\"2000\",\"3000\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x2\"},\"xaxis2\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"y\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[-5.0,0.0,5.0],\"domain\":[0.5549212598425197,0.9950787401574803],\"ticktext\":[\"-5\",\"0\",\"5\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y2\"},\"margin\":{\"r\":0,\"l\":0,\"b\":0,\"t\":20},\"height\":350,\"plot_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"paper_bgcolor\":\"rgba(255, 255, 255, 1.000)\"}, {showLink: false});\n",
       "\n",
       "    });\n",
       " </script>\n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Actual function at interpolation nodes\n",
    "y_new = f(S_new[:,1], S_new[:, 2])\n",
    "\n",
    "# Get coefficients\n",
    "c_new = Φ_new \\ y_new\n",
    "\n",
    "# Evaluate the new interpolant\n",
    "ygridf_new = linspace(-8, 8, 100)\n",
    "Sf_new = gridmake(agridf, ygridf_new)\n",
    "yf_new = f(Sf_new[:, 1], Sf_new[:, 2])\n",
    "interp_new = funeval(c_new, basis, Sf_new);\n",
    "\n",
    "# Plot the interpolant and the original function\n",
    "plt_a = plot(agridf, [interp_new[1:length(agridf)] yf_new[1:length(agridf)]], \n",
    "             label=[\"Interpolant\", \"Original\"]',\n",
    "             lw=2,\n",
    "             xlabel=\"a\")\n",
    "plt_y = plot(ygridf_new, [interp_new[5:length(agridf):end] yf_new[5:length(agridf):end]], \n",
    "             label=\"\",\n",
    "             lw=2,\n",
    "             xlabel=\"y\")\n",
    "plot(plt_a, plt_y, layout=(1, 2), size=(800, 350))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## An Economic Example\n",
    "\n",
    "In what follows, we will show how to use `BasisMatrices.jl` to solve the Aiyagari model (see the [lecture on QuantEcon](http://lectures.quantecon.org/jl/aiyagari.html) for background info). \n",
    "\n",
    "### The Method\n",
    "\n",
    "The Bellman equation we need to solve is:\n",
    "\n",
    "$$V(a, y) = \\max_{a'} u(c) + \\beta \\displaystyle \\sum_{y' \\in Y} P(y', y) V(a', y')$$\n",
    "subject to \n",
    "\\begin{align*}\n",
    "c + a' &= (1 + r)a + wy \\\\\n",
    "a' &> 0 \\\\\n",
    "c &> 0\n",
    "\\end{align*}\n",
    "\n",
    "To solve this, we need to first discretize the spaces of the two state variables. Let $N_a$ be the number of points for the grid over $a$ and $N_y$ be the number of grid points for $y$. Then, there will be $N = N_a N_y$ points on the state space $s$, which is a $N$ by 2 matrix of all the possible $(a, y)$ combinations. The value function at each $(a, y)$ combination can be approximated by the product of a basis function evaluated at that point and a vector of coefficients. We can stack these over all the points in $s$ to get an $N$ by $N$ basis matrix, $\\mathbf{\\Phi}(s)$. We can also express the expected value function (last term in the Bellman equation) in this way, with coefficients $c^e$. This gives us a system in $2N$ equations and $2N$ unknowns, which are the coefficients.\n",
    "\n",
    "\\begin{align*}\n",
    "\\mathbf{\\Phi}(s) c &= \\max_{a' \\in [0, (1+r)a + wy]} \\mathbf{u}(s, a') + \\beta \\mathbf{\\Phi}([a' s_2]) c^e \\\\\n",
    "\\mathbf{\\Phi}(s) c^e &= P \\otimes I_{N_a} \\mathbf{\\Phi}(s) c\n",
    "\\end{align*}\n",
    "\n",
    "We can solve using an iterative method: guess a set of coefficients $[c, c_e]'$, solve the right-hand side of the Bellman equation, and then invert $\\mathbf{\\Phi}(s)$ to update the guess of the coefficients until convergence. `BasisMatrices.jl` can efficiently set up and evaluate the basis matrices.\n",
    "\n",
    "### Code\n",
    "\n",
    "The following block of code simply defines the type that stores the parameters of the household problem, the state space, the basis matrices, and eventually, the coefficient solution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "type Household\n",
    "    r::Float64\n",
    "    w::Float64\n",
    "    β::Float64\n",
    "    y_chain::MarkovChain{Float64, Matrix{Float64}, Vector{Float64}}\n",
    "    amin::Float64\n",
    "    amax::Float64\n",
    "    asize::Int64\n",
    "    curv::Float64\n",
    "    order::Int64\n",
    "    P::Matrix{Float64}\n",
    "    ygrid0::Vector{Float64}\n",
    "    agrid0::Vector{Float64}\n",
    "    ygrid::Vector{Float64}\n",
    "    agrid::Vector{Float64}\n",
    "    s::Matrix{Float64}\n",
    "    Ny::Int64\n",
    "    Na::Int64\n",
    "    Ns::Int64\n",
    "    basis::Basis\n",
    "    bs::BasisMatrix\n",
    "    Φ::SparseMatrixCSC\n",
    "    Emat::SparseMatrixCSC\n",
    "    Φy::SparseMatrixCSC\n",
    "    c1::Vector{Float64}\n",
    "    c2::Vector{Float64}\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The constructor sets up the grids and the basis matrices. We use a Spline basis here that is linear in the $y$ dimension (we are assuming that the income shock can only take on the two values from the Markov chain) and of order 3 (cubic) in the $a$ dimension by default. \n",
    "\n",
    "Notice that we save the $y$ part of the interpolation in its `Direct` form, because we will need it later when we need to evaluate the basis matrix over new `a` spaces."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Household"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function Household(;r::Float64=0.03, w::Float64=1.35, β::Float64=0.96,\n",
    "                   y_chain::MarkovChain{Float64,Matrix{Float64},Vector{Float64}}=MarkovChain([0.67 0.33; 0.33 0.67], [0.5; 1.5]), \n",
    "                   amin::Float64=1e-10, amax::Float64=20.0, asize::Int64=7,\n",
    "                   curv::Float64=1.0, order::Int64=3)\n",
    "\n",
    "    # State space for idiosyncratic shocks\n",
    "    P = y_chain.p\n",
    "    ygrid0 = y_chain.state_values\n",
    "\n",
    "    # State space for assets (endogenous)\n",
    "    agrid0 = linspace(amin^curv, amax^curv, asize).^(1/curv)\n",
    "\n",
    "    # Define the basis over the state variables\n",
    "    basis = Basis(SplineParams(agrid0, 0, order),\n",
    "                  SplineParams(ygrid0, 0, 1))\n",
    "    s, (agrid, ygrid) = nodes(basis)\n",
    "    Ns, Na, Ny = size(s, 1), size(agrid, 1), size(ygrid, 1)\n",
    "\n",
    "    # Compute the basis matrix and expectations matrix\n",
    "    bs = BasisMatrix(basis, Direct(), s, [0 0])\n",
    "    Φ = convert(Expanded, bs).vals[1]\n",
    "    Emat = kron(P, speye(Na))*Φ\n",
    "\n",
    "    # Save just the y part of the interpolation\n",
    "    Φy = bs.vals[2] \n",
    "\n",
    "    # Initial guess for coefficients on value and expected value function\n",
    "    c1 = zeros(Ns, )\n",
    "    c2 = zeros(Ns, )\n",
    "    \n",
    "    return Household(r, w, β, y_chain, amin, amax, asize, curv, order, P,\n",
    "    ygrid0, agrid0, ygrid, agrid, s, Ny, Na, Ns, basis, bs, Φ, Emat, Φy, c1,\n",
    "    c2)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next is just a simple utility function that evaluates utility at all points in `s` for a given set of `ap` points representing the asset choice. It makes sure that if consumption is less than zero, the agent gets a very low utility (since `log` won't evaluate for negative values)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "utility (generic function with 1 method)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function utility(h::Household, s::Matrix{Float64}, ap::Vector{Float64})\n",
    "    c = (1 + h.r).*s[:, 1] + h.w.*s[:, 2] - ap\n",
    "    u = zeros(c)\n",
    "    for cc=1:length(c)\n",
    "        if c[cc] <= 0\n",
    "            u[cc] = -10000000\n",
    "        else\n",
    "            u[cc] = log(c[cc])\n",
    "        end\n",
    "    end\n",
    "    return u\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The function below evaluates the right-hand side of the value function on the state space for a given `ap`. This is where we take advantage of the basis matrix for `y` that was saved before. This function defines a new basis matrix for the `ap` vector that is entered. Then to evaluate the matrix at these new points, it just needs to take tensor product of the separate basis matrices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "value (generic function with 1 method)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function value(h::Household, s::Matrix{Float64}, ap::Vector{Float64})\n",
    "\n",
    "    # Compute flow payoff\n",
    "    u = utility(h, s, ap)\n",
    "\n",
    "    # Basis matrix for continuation value\n",
    "    bs_new = BasisMatrix(h.basis[1], Direct(), ap, 0)\n",
    "    Φa = bs_new.vals[1]\n",
    "    Φapy = row_kron(h.Φy, Φa)\n",
    "\n",
    "    # Compute value\n",
    "    v1 = u + h.β*Φapy*h.c2\n",
    "\n",
    "    return v1\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following solves the maximization problem over next period's assets using `golden_method`. In the end, it returns the maximized value of the value function, the optimal asset holdings, and the expected value function given the current coefficient guess."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "opt_value (generic function with 1 method)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function opt_value(h::Household, s::Matrix{Float64})\n",
    "\n",
    "    # Solve maximization problem\n",
    "    lower_bound = zeros(size(s, 1), )\n",
    "    upper_bound = (1 + h.r).*s[:, 1] + h.w.*s[:, 2]\n",
    "    f(ap) = value(h, s, ap)\n",
    "    ap, v1 = golden_method(f, lower_bound, upper_bound)\n",
    "\n",
    "    # Compute expected value function\n",
    "    v2 = h.Emat*h.c1\n",
    "\n",
    "    return v1, v2, ap\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The last two functions simply iterate on the system and update the coefficient guesses (`iteration!`) and then repeat until convergence (`vfi!`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "vfi! (generic function with 1 method)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function iteration!(h::Household)\n",
    "\n",
    "    # Compute values\n",
    "    s = h.s\n",
    "    v1, v2, ap = opt_value(h, s)\n",
    "\n",
    "    # Update coefficients\n",
    "    c1 = h.Φ\\v1\n",
    "    c2 = h.Φ\\v2\n",
    "    h.c1 = c1\n",
    "    h.c2 = c2\n",
    "\n",
    "    Void\n",
    "end\n",
    "\n",
    "function vfi!(h; tol::Float64=1e-8, print::Bool=true)\n",
    "\n",
    "    i = 0\n",
    "    dc = 1\n",
    "    while dc > tol\n",
    "        i = i + 1\n",
    "        c_old = [h.c1; h.c2]\n",
    "        iteration!(h)\n",
    "        c_new = [h.c1; h.c2]\n",
    "        dc = norm(c_new - c_old)/norm(c_old)\n",
    "        if print\n",
    "            println(\"Iteration $(i); distance of $(dc)\")\n",
    "        end\n",
    "    end\n",
    "    println(\"Converged in $(i) iterations.\")\n",
    "    Void\n",
    "end\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's set up a `Household` object, solve the problem, and plot the asset and consumption policies on the state space."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Converged in 699 iterations.\n"
     ]
    }
   ],
   "source": [
    "h = Household()\n",
    "vfi!(h; print=false)\n",
    "v1, v2, ap = opt_value(h, h.s)\n",
    "c = (1 + h.r).*h.s[:, 1] + h.w.*h.s[:, 2] - ap;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"7f41f541-aafd-4f8d-ba1a-a639f185e791\" class=\"plotly-graph-div\"></div>\n",
       "\n",
       "<script>\n",
       "    window.PLOTLYENV=window.PLOTLYENV || {};\n",
       "    window.PLOTLYENV.BASE_URL=\"https://plot.ly\";\n",
       "    require(['plotly'], function(Plotly) {\n",
       "        Plotly.newPlot('7f41f541-aafd-4f8d-ba1a-a639f185e791', [{\"yaxis\":\"y\",\"y\":[1.0812530346620202e-14,0.7165698822516557,2.6672692067880908,5.818132897074084,9.035178790826343,12.280271727221383,15.539661200950553,17.717786027528415,18.807938038763428],\"showlegend\":true,\"name\":\"Low income\",\"type\":\"scatter\",\"xaxis\":\"x\",\"line\":{\"width\":1,\"dash\":\"solid\",\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\"},\"x\":[1.0e-10,1.1111111112055554,3.3333333334166664,6.666666666733332,10.00000000005,13.333333333366667,16.666666666683337,18.888888888894446,20.0],\"mode\":\"lines\"},{\"yaxis\":\"y\",\"y\":[0.7969325419838118,1.7806158345341878,3.8394686246411305,7.0388629073664495,10.272393996089551,13.527327483872035,16.79196456336068,18.9727835576475,20.06420981292976],\"showlegend\":true,\"name\":\"High income\",\"type\":\"scatter\",\"xaxis\":\"x\",\"line\":{\"width\":1,\"dash\":\"solid\",\"color\":\"rgba(227, 111, 71, 1.000)\",\"shape\":\"linear\"},\"x\":[1.0e-10,1.1111111112055554,3.3333333334166664,6.666666666733332,10.00000000005,13.333333333366667,16.666666666683337,18.888888888894446,20.0],\"mode\":\"lines\"},{\"yaxis\":\"y2\",\"y\":[0.6750000001029893,1.1028745622900664,1.4410641266310753,1.7235337696612483,1.9398212092251566,2.128061606146286,2.302005465733286,2.4127695280328645,2.467061961236574],\"showlegend\":false,\"name\":\"\",\"type\":\"scatter\",\"xaxis\":\"x2\",\"line\":{\"width\":1,\"dash\":\"solid\",\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\"},\"x\":[1.0e-10,1.1111111112055554,3.3333333334166664,6.666666666733332,10.00000000005,13.333333333366667,16.666666666683337,18.888888888894446,20.0],\"mode\":\"lines\"},{\"yaxis\":\"y2\",\"y\":[1.2280674581191884,1.3888286100075347,1.6188647087780361,1.852803759368883,2.052606003961948,2.2310058494956326,2.3997021033231576,2.507771997913782,2.560790187070239],\"showlegend\":false,\"name\":\"\",\"type\":\"scatter\",\"xaxis\":\"x2\",\"line\":{\"width\":1,\"dash\":\"solid\",\"color\":\"rgba(227, 111, 71, 1.000)\",\"shape\":\"linear\"},\"x\":[1.0e-10,1.1111111112055554,3.3333333334166664,6.666666666733332,10.00000000005,13.333333333366667,16.666666666683337,18.888888888894446,20.0],\"mode\":\"lines\"}],\n",
       "               {\"yaxis\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"Assets tomorrow\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[5.0,10.0,15.0,20.0],\"domain\":[0.07581474190726165,0.9901574803149606],\"ticktext\":[\"5\",\"10\",\"15\",\"20\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x\"},\"annotations\":[],\"width\":900,\"showlegend\":true,\"legend\":{\"bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"y\":1.0,\"font\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"bordercolor\":\"rgba(0, 0, 0, 1.000)\",\"x\":1.0},\"xaxis\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"Assets today\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[5.0,10.0,15.0,20.0],\"domain\":[0.041102848255079226,0.5005638184115875],\"ticktext\":[\"5\",\"10\",\"15\",\"20\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y\"},\"yaxis2\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"Consumption\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[1.0,2.0],\"domain\":[0.07581474190726165,0.9901574803149606],\"ticktext\":[\"1\",\"2\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x2\"},\"xaxis2\":{\"type\":\"-\",\"titlefont\":{\"size\":15,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"title\":\"Assets today\",\"tickfont\":{\"size\":11,\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\"},\"ticks\":\"inside\",\"tickmode\":\"array\",\"showgrid\":true,\"tickvals\":[5.0,10.0,15.0,20.0],\"domain\":[0.536164576650141,0.9956255468066492],\"ticktext\":[\"5\",\"10\",\"15\",\"20\"],\"tickangle\":0,\"zeroline\":false,\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"tickcolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y2\"},\"margin\":{\"r\":0,\"l\":0,\"b\":0,\"t\":20},\"height\":400,\"plot_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"paper_bgcolor\":\"rgba(255, 255, 255, 1.000)\"}, {showLink: false});\n",
       "\n",
       "    });\n",
       " </script>\n"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot_a = plot(h.agrid, reshape(ap, h.Na, 2), labels=[\"Low income\" \"High income\"],\n",
    "xlabel=\"Assets today\", ylabel=\"Assets tomorrow\")\n",
    "plot_c = plot(h.agrid, reshape(c, h.Na, 2), labels=[\"\" \"\"],\n",
    "xlabel=\"Assets today\", ylabel=\"Consumption\")\n",
    "plot(plot_a, plot_c, layout=(1,2), size=(900,400))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.5.0",
   "language": "julia",
   "name": "julia-0.5"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
